/*
 * This file contains handlers for all the scalar objects for
 * the quantum sip server.
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include "QuantumSipServerObjects.h"

#include "../../dprint.h"
#include "../../lib/kcore/statistics.h"
#include "../../config.h"
#include "snmpstats_globals.h"
#include "utilities.h"
#include "alarm_checks.h"

/*
 * Initializes the QuantumSipServer module
 */
void init_QuantumSipServerObjects(void)
{

    const oid       OnlineUserCount_oid[] =
        { 1, 3, 6, 1, 4, 1, 3001, 2, 1 };

    const oid       CurrentDialogCount_oid[] =
        { 1, 3, 6, 1, 4, 1, 3001, 3, 1 };

    const oid       TotalDialogCount_oid[] =
        { 1, 3, 6, 1, 4, 1, 3001, 3, 3 };

    const oid       FailedDialogCount_oid[] =
        { 1, 3, 6, 1, 4, 1, 3001, 3, 4 };


    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("OnlineUserCount", handle_OnlineUserCount,
                             OnlineUserCount_oid,
                             OID_LENGTH(OnlineUserCount_oid),
                             HANDLER_CAN_RONLY));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("CurrentDialogCount",
                             handle_CurrentDialogCount,
                             CurrentDialogCount_oid,
                             OID_LENGTH(CurrentDialogCount_oid),
                             HANDLER_CAN_RONLY));

    /*
    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("TotalDialogCount", handle_TotalDialogCount,
                             TotalDialogCount_oid,
                             OID_LENGTH(TotalDialogCount_oid),
                             HANDLER_CAN_RONLY));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("FailedDialogCount", handle_FailedDialogCount,
                             FailedDialogCount_oid,
                             OID_LENGTH(FailedDialogCount_oid),
                             HANDLER_CAN_RONLY));
    */
}

int handle_OnlineUserCount(netsnmp_mib_handler *handler,
                       netsnmp_handler_registration *reginfo,
                       netsnmp_agent_request_info *reqinfo,
                       netsnmp_request_info *requests)
{
    /*
     * We are never called for a GETNEXT if it's registered as a
     * "instance", as it's "magically" handled for us.
     */
    /*
     * a instance handler also only hands us one request at a time, so
     * we don't need to loop over a list of requests; we'll only get one.
     */
    int onlineUserCount = get_statistic("registered_users");
    switch (reqinfo->mode) {

    case MODE_GET:
        snmp_set_var_typed_value(
            requests->requestvb,
            ASN_UNSIGNED,
            (u_char *) &onlineUserCount,
            sizeof(int)
        );
        break;


    default:
        /*
         * we should never get here, so this is a really bad error
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_OnlineUserCount\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

int handle_CurrentDialogCount(netsnmp_mib_handler *handler,
                          netsnmp_handler_registration *reginfo,
                          netsnmp_agent_request_info *reqinfo,
                          netsnmp_request_info *requests)
{

    // Statistic provided by dialog module
    int dialogCount = get_statistic("active_dialogs");
    switch (reqinfo->mode) {

    case MODE_GET:
        snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
                                (u_char *) &dialogCount,
                                sizeof(int));
        break;


    default:
        snmp_log(LOG_ERR,
                 "unknown mode (%d) in handle_CurrentDialogCount\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

int handle_TotalDialogCount(netsnmp_mib_handler *handler,
                        netsnmp_handler_registration *reginfo,
                        netsnmp_agent_request_info *reqinfo,
                        netsnmp_request_info *requests)
{

    // Statistic provided by dialog module
    int dialogCount = get_statistic("processed_dialogs");
    switch (reqinfo->mode) {

    case MODE_GET:
         snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
                                (u_char *) &dialogCount,
                                sizeof(int));
        break;

    default:
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_TotalDialogCount\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

int handle_FailedDialogCount(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *reqinfo,
                         netsnmp_request_info *requests)
{
    // Statistic provided by dialog module
    int dialogCount = get_statistic("failed_dialogs");
    switch (reqinfo->mode) {

    case MODE_GET:
         snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
                                (u_char *) &dialogCount,
                                sizeof(int));
        break;


    default:
        /*
         * we should never get here, so this is a really bad error
         */
        snmp_log(LOG_ERR,
                 "unknown mode (%d) in handle_FailedDialogCount\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
